﻿// ====================================
// Created By Angie.D 2010.6
// ====================================

using System;
using System.Collections.Generic;
using System.Text;

using System.Text.RegularExpressions;
using System.Threading;
using System.Xml.Serialization;

namespace WOWMultiPlay
{
    public static class mpTimer
    {
        public static void Sleep(mpTime mpTime)
        {
            Sleep_Millisecond(mpTime.Millisecond);
            Sleep_Second(mpTime.Second);
            Sleep_Minute(mpTime.Minute);
            Sleep_Hour(mpTime.Hour);
        }

        private static void Sleep_Millisecond(int period)
        {
            DateTime EndTime = DateTime.Now.AddMilliseconds(period);

            while (DateTime.Now < EndTime)
            {
                Thread.Sleep(1);
            }
        }

        private static void Sleep_Second(int period)
        {
            DateTime EndTime = DateTime.Now.AddSeconds(period);

            while (DateTime.Now < EndTime)
            {
                Thread.Sleep(50);
            }
        }

        private static void Sleep_Minute(int period)
        {
            DateTime EndTime = DateTime.Now.AddMinutes(period);

            while (DateTime.Now < EndTime)
            {
                Thread.Sleep(500);
            }
        }

        private static void Sleep_Hour(int period)
        {
            DateTime EndTime = DateTime.Now.AddMinutes(period);

            while (DateTime.Now < EndTime)
            {
                Thread.Sleep(5000);
            }
        }
    }

    [XmlRootAttribute("mpTime", IsNullable = false)]
    public class mpTime
    {
        public int Millisecond;
        public int Second;
        public int Minute;
        public int Hour;

        [XmlIgnore]
        private const string MillisecondKeyword = "ms";
        [XmlIgnore]
        private const string SecondKeyword = "s";
        [XmlIgnore]
        private const string MinuteKeyword = "m";
        [XmlIgnore]
        private const string HourKeyword = "h";

        public mpTime() { }

        public mpTime(string mpTimeStr)
        {
            Regex TimeIntervalRegex = new Regex("(\\d{1,})(\\D{1,})");
            MatchCollection TimeIntervalMatches = TimeIntervalRegex.Matches(mpTimeStr);

            if (TimeIntervalMatches.Count > 0)
            {
                for (int i = 0; i < TimeIntervalMatches.Count; i++)
                {
                    switch (TimeIntervalMatches[i].Groups[2].Value.ToLower())
                    {
                        case MillisecondKeyword:
                            this.Millisecond = Convert.ToInt32(TimeIntervalMatches[i].Groups[1].Value);
                            break;
                        case SecondKeyword:
                            this.Second = Convert.ToInt32(TimeIntervalMatches[i].Groups[1].Value);
                            break;
                        case MinuteKeyword:
                            this.Minute = Convert.ToInt32(TimeIntervalMatches[i].Groups[1].Value);
                            break;
                        case HourKeyword:
                            this.Hour = Convert.ToInt32(TimeIntervalMatches[i].Groups[1].Value);
                            break;
                        default:
                            throw new Exception("Invalid time format.");
                    }
                }
            }
        }

        public override string ToString()
        {
            StringBuilder sbOut = new StringBuilder();

            if (Hour > 0)
            {
                sbOut.Append(Hour.ToString());
                sbOut.Append(HourKeyword);
            }
            if (Minute > 0)
            {
                sbOut.Append(Minute.ToString());
                sbOut.Append(MinuteKeyword);
            }
            if (Second > 0)
            {
                sbOut.Append(Second.ToString());
                sbOut.Append(SecondKeyword);
            }
            if (Millisecond > 0)
            {
                sbOut.Append(Millisecond.ToString());
                sbOut.Append(MillisecondKeyword);
            }

            return sbOut.ToString();
        }
    }
}
